package com.cp.yn.activity

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.ViewPager2
import com.common.base.base.BaseActivity
import com.common.base.base.adapter.AdapterViewPage2
import com.cp.yn.R.id
import com.cp.yn.databinding.ActivityMainBinding
import com.cp.yn.fragment.HomeFragment
import com.cp.yn.fragment.MineFragment
import com.cp.yn.fragment.OrderFragment
import com.cp.yn.manager.RouterManager
import com.didi.drouter.annotation.Router
import com.google.android.material.navigation.NavigationBarView

@Router(path = RouterManager.Path.APP_Main)
class MainActivity : BaseActivity<ActivityMainBinding>() {
    var mFragment = mutableListOf<Fragment>()

    override fun viewBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBottomNavView()
    }

    private fun initBottomNavView() {
        mFragment.add(HomeFragment())
        mFragment.add(OrderFragment())
        mFragment.add(MineFragment())
        mFragment.add(MineFragment())

        viewBinding.apply {
            viewPage2.adapter =
                AdapterViewPage2(baseActivity, mFragment)
            viewPage2.orientation = ViewPager2.ORIENTATION_HORIZONTAL
            viewPage2.isUserInputEnabled = true
            viewPage2.offscreenPageLimit = mFragment.size
            viewPage2.setPageTransformer(CompositePageTransformer())
            viewPage2.registerOnPageChangeCallback(object :
                ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    when (position) {
                        0 -> bottomNavigationView.selectedItemId = id.tab_main_tools
                        1 -> bottomNavigationView.selectedItemId = id.tab_main_watermark
                        2 -> bottomNavigationView.selectedItemId = id.tab_main_agreement
                        3 -> bottomNavigationView.selectedItemId = id.tab_main_setting
                    }

                }
            })
            bottomNavigationView.labelVisibilityMode = NavigationBarView.LABEL_VISIBILITY_LABELED
            bottomNavigationView.setOnItemSelectedListener { item ->
                when (item.itemId) {
                    id.tab_main_tools -> viewPage2.setCurrentItem(0, false)
                    id.tab_main_watermark -> viewPage2.setCurrentItem(1, false)
                    id.tab_main_agreement -> viewPage2.setCurrentItem(2, false)
                    id.tab_main_setting -> viewPage2.setCurrentItem(3, false)
                }
                true
            }
            viewPage2.currentItem = 0
        }

    }


}